﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        PurchaseRequestBLL bllPrReq = new PurchaseRequestBLL();
        PurchaseRequestInfo infoPrReq = new PurchaseRequestInfo();
        IBLLMasterPurchaseMethod bllMasPcMethod = new MasterPurchaseMethodBLL();
        PurchaseRequestItemBLL bllPcReqItem = new PurchaseRequestItemBLL();
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();
        private const int ItemsPerRequest = 10;
        double totalPrice = 0;
        int nCount = 0;
        IBLLDocument bllPprFile = new DocumentBLL();
        IBLLSysDurationCount bllDurationCount = new SysDurationCountBLL();
        IBLLSysDurationType bllDurationType = new SysDurationTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPurchaseMethod();
                LoadDurationType();
                LoadDurationCount();

                LoadDetail();
                GetKeyValue();
                rdbDuration.Attributes.Add("onclick", "return ChangeDurationRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
                rdbDate.Attributes.Add("onclick", "return ChangeDateRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
            }
            else
            {
                this.infoPrReq = (PurchaseRequestInfo)Session["infoPrReq"];
            }
            BasicDatePicker1.DisplayType = BasicFrame.WebControls.DatePickerDisplayType.Image;
            BasicDatePicker2.DisplayType = BasicFrame.WebControls.DatePickerDisplayType.Image;
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            if (RadTabStrip1.SelectedTab.Value == "Detail")
            {
                LoadDetail();
                GetKeyValue();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Item")
            {
                //LoadGridItem();
            }
            else if (RadTabStrip1.SelectedTab.Value == "File")
            {
                //LoadGridFile();
            }
            else if (RadTabStrip1.SelectedTab.Value == "Report")
            {

            }
            else if (RadTabStrip1.SelectedTab.Value == "Progress")
            {
                //LoadGridApprove();
                //LoadGridInform();
                //LoadGridRequest();
                //LoadGridPublishing();
            }
        }

        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip(string elementID, UpdatePanel panel)
        {
            Control ctrl = Page.LoadControl("../UserControl/TIP_Purchase_Request_Confirm.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void ManageDisplay(string Status)
        {
            switch (Status)
            {
                case "A":
                case "P":
                    // tab ข้อมูลหลัก
                    panelCommand_Detail.Visible = false;
                    // tab เอกสารแนบ
                    panelCommand_File.Visible = false;
                    Forward.Visible = false;
                    Reject.Visible = true;
                    break;
                case "C":
                    // tab ข้อมูลหลัก
                    panelCommand_Detail.Visible = false;
                    // tab เอกสารแนบ
                    panelCommand_File.Visible = false;
                    Forward.Visible = false;
                    Reject.Visible = false;
                    break;
                case "N":
                case "R":
                case "W":
                    // tab ข้อมูลหลัก
                    panelCommand_Detail.Visible = true;
                    // tab เอกสารแนบ
                    panelCommand_File.Visible = true;
                    Forward.Visible = true;
                    Reject.Visible = true;
                    break;
            }
        }

        //-----------------------------------------------------------------------------------------------

        #region tab ข้อมูลหลัก

        protected void LoadDetail()
        {
            int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            this.infoPrReq = bllPrReq.GetPurchaseRequest(seq);
            Session["infoPrReq"] = this.infoPrReq;
        }

        protected void LoadPurchaseMethod()
        {
            int seq = 41;// Convert.ToInt32(Request.QueryString["Seq"]);
            IList<MasterPurchaseMethodInfo> infPprMethod = bllMasPcMethod.GetMasterPurchaseMethodByPrSeq(seq);
            cbPurchaseMethod.DataSource = infPprMethod;
            cbPurchaseMethod.DataBind();
        }

        protected void LoadDurationType()
        {
            IList<SysDurationTypeInfo> infoList = bllDurationType.GetSysDurationType();
            cbDurationType.DataSource = infoList;
            cbDurationType.DataBind();
        }

        protected void LoadDurationCount()
        {
            IList<SysDurationCountInfo> infoList = bllDurationCount.GetSysDurationCount();
            cbDurationCount.DataSource = infoList;
            cbDurationCount.DataBind();
        }

        public void GetKeyValue()
        {
            /*------------------ add data to text --------------------------*/
            lblYearType.Text = this.infoPrReq.InfoYearType.Name + " : ";
            lblYear.Text = this.infoPrReq.BgmYear.ToString();
            lblPurchaseRule.Text = this.infoPrReq.InfoPurchaseRule.Name;
            lblPurchaseType.Text = this.infoPrReq.InfoPurchaseType.Name;
            lblDate.Text = ((DateTime)this.infoPrReq.PurchaseDate).ToString("dd/MM/yyyy");
            lblName.Text = this.infoPrReq.Name;
            lblReason.Text = this.infoPrReq.Reason;
            lblPurchaseMethod.Text = this.infoPrReq.InfoPurchaseMethod.Name;
            lblPurchaseMethodReason.Text = this.infoPrReq.MethodReason;
            if (this.infoPrReq.RequireDuration != null)
            {
                lblDuration.Text = "ภายใน " + this.infoPrReq.RequireDuration + " " + this.infoPrReq.InfoDurationType.Name + " นับจาก " + this.infoPrReq.InfoDurationCount.Name;
            }
            else if (this.infoPrReq.RequireDate != null)
            {
                lblDuration.Text = "ภายในวันที่ " + ((DateTime)this.infoPrReq.RequireDate).ToString("dd/MM/yyyy"); ;
            }
            else
            {
                lblDuration.Text = "";
            }

            lblStatus.Text = this.infoPrReq.InfoRecordStatus.Name;
            lblProgress.Text = this.infoPrReq.InfoProgress.Name;

            /*------------------- show and hide text --------------------*/
            lblDate.Visible = true;
            txtDate.Visible = false;
            BasicDatePicker1.Visible = false;
            Label1.Visible = false;
            lblName.Visible = true;
            txtName.Visible = false;
            Label2.Visible = false;
            lblReason.Visible = true;
            txtReason.Visible = false;
            lblPurchaseMethod.Visible = true;
            lblPurchaseMethodReason.Visible = true;
            cbPurchaseMethod.Visible = false;
            lblWarningPcMethod.Visible = false;
            txtPurchaseMethodReason.Visible = false;
            lblWarningPcMethodReason.Visible = false;
            lblDuration.Visible = true;
            rdbDuration.Visible = false;
            panelDuration.Visible = false;
            rdbDate.Visible = false;
            panelDateReq.Visible = false;
            panelDateReqPick.Visible = false;

            /*---------------------- show and hide Button -----------------*/
            Edit_Detail.Visible = true;
            EditSave_Detail.Visible = false;
            Cancel_Detail.Visible = false;

            /*---------------------- show and hide Header ----------------*/
            if (this.infoPrReq.InfoPurchaseMethod.Seq == null)
            {
                panelWarning.Visible = true;
                panelHidden.Visible = false;
            }
            else
            {
                panelWarning.Visible = false;
                panelHidden.Visible = true;
            }

            ManageDisplay(this.infoPrReq.InfoRecordStatus.Status);
            //cmdFac.ViewCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
        }

        protected void Edit_Detail_Click(object sender, EventArgs e)
        {
            Edit_Detail.Visible = false;
            EditSave_Detail.Visible = true;
            Cancel_Detail.Visible = true;

            lblDate.Visible = false;
            lblName.Visible = false;
            lblReason.Visible = false;
            lblPurchaseMethod.Visible = false;
            lblPurchaseMethodReason.Visible = false;
            lblDuration.Visible = false;

            txtDate.Visible = true;
            BasicDatePicker1.Visible = true;
            Label1.Visible = true;
            txtName.Visible = true;
            Label2.Visible = true;
            txtReason.Visible = true;
            cbPurchaseMethod.Visible = true;
            lblWarningPcMethod.Visible = true;
            txtPurchaseMethodReason.Visible = true;
            lblWarningPcMethodReason.Visible = true;

            rdbDuration.Visible = true;
            panelDuration.Visible = true;
            rdbDate.Visible = true;
            panelDateReq.Visible = true;
            panelDateReqPick.Visible = true;

            this.infoPrReq = (PurchaseRequestInfo)Session["infoPrReq"];
            txtDate.Text = ((DateTime)this.infoPrReq.PurchaseDate).ToString("dd/MM/yyyy");
            txtName.Text = this.infoPrReq.Name;
            txtReason.Text = this.infoPrReq.Reason;
            cbPurchaseMethod.Text = this.infoPrReq.InfoPurchaseMethod.Name;
            cbPurchaseMethod.SelectedValue = this.infoPrReq.InfoPurchaseMethod.Seq.ToString();
            txtPurchaseMethodReason.Text = this.infoPrReq.MethodReason;
            if (this.infoPrReq.RequireDuration != null)
            {
                rdbDuration.Checked = true;
                rdbDate.Checked = false;
                txtDurationReq.Text = this.infoPrReq.RequireDuration.ToString();
                cbDurationType.SelectedValue = this.infoPrReq.InfoDurationType.Seq.ToString();
                cbDurationCount.SelectedValue = this.infoPrReq.InfoDurationCount.Seq.ToString();
                RadManager1.ResponseScripts.Add(@"ChangeDurationRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
            }
            else if (this.infoPrReq.RequireDate != null)
            {
                rdbDate.Checked = true;
                rdbDuration.Checked = false;
                txtDateReq.Text = ((DateTime)this.infoPrReq.RequireDate).ToString("dd/MM/yyyy");
                RadManager1.ResponseScripts.Add(@"ChangeDateRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
            }
            else
            {
                rdbDuration.Checked = true;
                rdbDate.Checked = false;
                RadManager1.ResponseScripts.Add(@"ChangeDurationRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
            }
        }

        protected void EditSave_Detail_Click(object sender, EventArgs e)
        {
            try
            {
                int seq = 41;//Convert.ToInt32(Request.QueryString["Seq"]);
                Nullable<DateTime> PurchaseDate = (txtDate.Text != "") ? (Nullable<DateTime>)Convert.ToDateTime(txtDate.TextWithLiterals) : null;
                string Name = txtName.Text;
                string Reason = txtReason.Text;
                int PurchaseMethodSeq = Convert.ToInt32(cbPurchaseMethod.SelectedValue.ToString());
                string MethodReason = txtPurchaseMethodReason.Text;
                Nullable<int> RequireDuration = null;
                Nullable<int> DurationTypeSeq = null;
                Nullable<int> DurationCountSeq = null;
                Nullable<DateTime> RequireDate = null;
                if (rdbDuration.Checked)
                {
                    if (txtDurationReq.Text.Length > 0)
                    {
                        RequireDuration = Convert.ToInt32(txtDurationReq.Text);
                        DurationTypeSeq = Convert.ToInt32(cbDurationType.SelectedValue.ToString());
                        DurationCountSeq = Convert.ToInt32(cbDurationCount.SelectedValue.ToString());
                    }
                }
                else if (rdbDate.Checked)
                {
                    if (txtDateReq.Text.Length > 0)
                    {
                        RequireDate = Convert.ToDateTime(txtDateReq.TextWithLiterals);
                    }
                }

                //bllPrReq.UpdatePurchaseRequest(seq, PurchaseDate, Name, Reason, PurchaseMethodSeq, MethodReason, RequireDuration, DurationTypeSeq, DurationCountSeq, RequireDate);

                Edit_Detail.Visible = true;
                EditSave_Detail.Visible = false;
                Cancel_Detail.Visible = false;

                LoadDetail();
                GetKeyValue();
                Cancel_Detail_Click(null, null);
                panelWarning.Visible = false;
                panelHidden.Visible = true;
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
            }
        }

        protected void Cancel_Detail_Click(object sender, EventArgs e)
        {
            Edit_Detail.Visible = true;
            EditSave_Detail.Visible = false;
            Cancel_Detail.Visible = false;

            lblDate.Visible = true;
            lblName.Visible = true;
            lblReason.Visible = true;
            lblPurchaseMethod.Visible = true;
            lblPurchaseMethodReason.Visible = true;
            lblDuration.Visible = true;

            txtDate.Visible = false;
            BasicDatePicker1.Visible = false;
            Label1.Visible = false;
            txtName.Visible = false;
            Label2.Visible = false;
            txtReason.Visible = false;
            cbPurchaseMethod.Visible = false;
            lblWarningPcMethod.Visible = false;
            txtPurchaseMethodReason.Visible = false;
            lblWarningPcMethodReason.Visible = false;
            rdbDuration.Visible = false;
            panelDuration.Visible = false;
            rdbDate.Visible = false;
            panelDateReq.Visible = false;
            panelDateReqPick.Visible = false;
        }

        protected void btnEditPcMethod_Click(object sender, EventArgs e)
        {
            RadTabStrip1.SelectedIndex = 0;
            RadPageView1.Selected = true;
            Edit_Detail_Click(null, null);
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab รายการพัสดุ

        protected void LoadGridItem()
        {
            //int seq = Convert.ToInt32(Request.QueryString["Seq"]);
            //IList<PurchaseRequestItemInfo> infoPcReqItem = bllPcReqItem.GetPurchaseRequestItem(seq);
            IList<PurchaseRequestItemInfo> infoPcReqItem = bllPcReqItem.GetPurchaseRequestItem(7);
            Grid_Item.DataSource = infoPcReqItem;
            GridGroupByExpression geExpense = new GridGroupByExpression(Grid_Item.Columns[0]);
            Grid_Item.MasterTableView.GroupByExpressions.Add(geExpense);

            Grid_Item.DataBind();
        }

        protected void Grid_Item_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridItem();
        }

        protected void Grid_Item_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (nCount == 0)
            {
                totalPrice = 0;
            }
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                nCount++;
                double qty = 0;
                double price = 0;
                GridDataItem dataBoundItem = e.Item as GridDataItem;

                if (dataBoundItem["Qty"].Text.Length > 0)
                {
                    qty = double.Parse(dataBoundItem["Qty"].Text);
                }
                if (((Label)(dataBoundItem["Price"].FindControl("lblPrice"))).Text.Length > 0)
                {
                    price = double.Parse(((Label)(dataBoundItem["Price"].FindControl("lblPrice"))).Text);
                }

                totalPrice += (qty * price);
            }
            else if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["InfoItem.Descr"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>วงเงินจัดหาทั้งหมด :</span> "));
                ((Label)footer["Price"].FindControl("lblTotalPrice")).Text = totalPrice.ToString("###,###,##0.00");
                nCount = 0;
            }
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab คณะกรรมการ

        protected void Grid_Committee_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridCommittee();
        }

        protected void rblCommitteeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelInternal.Visible = (rblCommitteeType.SelectedValue == "1");
            panelExternal.Visible = (rblCommitteeType.SelectedValue == "2");
        }

        protected void cbCommitteeInternalName_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<HrmEmployeeInfo> infoList = bllEmployee.GetHrmEmployee(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cbCommitteeInternalName.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdJobSite = new TableCell();
                Label lblJobSite = new Label();
                lblJobSite.Text = infoList[i].InfoJobSite.Name;
                tdJobSite.Width = Unit.Pixel(200);
                tdJobSite.Controls.Add(lblJobSite);
                tr.Cells.Add(tdJobSite);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdPosition = new TableCell();
                Label lblPosition = new Label();
                lblPosition.Text = infoList[i].InfoPosition.Name;
                tdPosition.Width = Unit.Pixel(150);
                tdPosition.Controls.Add(lblPosition);
                tr.Cells.Add(tdPosition);
                TableCell tdClass = new TableCell();
                Label lblClass = new Label();
                lblClass.Text = infoList[i].InfoClass.Name;
                tdClass.Width = Unit.Pixel(190);
                tdClass.Controls.Add(lblClass);
                tr.Cells.Add(tdClass);
                ((Table)cbCommitteeInternalName.Items[j].FindControl("tableCommmitteeInternalName")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/TIP_Purchase_Request_Committee_History.ascx"));
        }

        protected void Delete_Committee_Click(object sender, EventArgs e)
        {
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab ผู้ขาย/รับจ้าง

        protected void Grid_Seller_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridCommittee();
        }

        protected void Delete_Seller_Click(object sender, EventArgs e)
        {
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab สรุปรายการคัดเลือก

        protected void Grid_Judgement_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridCommittee();
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab เอกสารแนบ

        protected void LoadGridFile()
        {
            int seq = Convert.ToInt32(Request.QueryString["Seq"]);
            Grid_File.DataSource = bllPprFile.GetDocument(seq, "icm_purchase_request_file", "ICM_PR_SEQ", "icm_pr_file_seq");
            Grid_File.DataBind();

            cmdFac.GridCommand(panelList_File, panelGrid_File, panelCommand_File);
        }

        protected void Grid_File_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridFile();
        }

        protected void AddNew_File_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator valFileName = new RequiredFieldValidator();
            valFileName.ID = "ValtxtFileName";
            valFileName.ControlToValidate = "txtFileName";
            valFileName.SetFocusOnError = true;
            valFileName.Display = ValidatorDisplay.Dynamic;
            valFileName.ErrorMessage = "* กรุณาระบุชื่อไฟล์ *";
            panelValtxtFileName.Controls.Add(valFileName);
            RequiredFieldValidator valAttachment = new RequiredFieldValidator();
            valAttachment.ID = "Valattachment";
            valAttachment.ControlToValidate = "attachment";
            valAttachment.SetFocusOnError = true;
            valAttachment.Display = ValidatorDisplay.Dynamic;
            valAttachment.ErrorMessage = "* กรุณาระบุที่อยู่ไฟล์ *";
            panelValattachment.Controls.Add(valAttachment);
            cmdFac.AddNewCommand(panelList_File, panelGrid_File, panelCommand_File);
        }

        protected void AddNewSave_File_Click(object sender, EventArgs e)
        {
            int seq = Convert.ToInt32(Request.QueryString["Seq"]);
            FileInfo infoFile = new FileInfo(hdSourceFile.Value);
            DocumentInfo info = new DocumentInfo();
            RecordInfo infoRecord = new RecordInfo();
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.File_info = infoFile;
            info.Name = txtFileName.Text;
            info.Dest_path = hdSourceFile.Value;
            info.InfoRecord = infoRecord;

            try
            {
                bllPprFile.InsertFileToDB(info, "ICM_PURCHASE_REQUEST_FILE", "ICM_PR_SEQ", seq);
                LoadGridFile();
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
        }

        protected void Delete_File_Click(object sender, EventArgs e)
        {
            IList<DocumentInfo> infoList = new List<DocumentInfo>();
            foreach (GridDataItem data in Grid_File.MasterTableView.GetSelectedItems())
            {
                DocumentInfo info = new DocumentInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllPprFile.DeleteFileFromDB(infoList, "ICM_PURCHASE_REQUEST_FILE", "ICM_PR_FILE_SEQ");
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadGridFile();
        }

        protected void ShowGrid_File_Click(object sender, EventArgs e)
        {
            LoadGridFile();
        }

        protected void OnAjaxUpdate_History(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip_History(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip_History(string elementID, UpdatePanel panel)
        {
            int seq = Convert.ToInt32(Request.QueryString["Seq"]);
            Session["PrSeq"] = seq;
            Control ctrl = Page.LoadControl("../UserControl/TIP_Purchase_Request_File_History.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        #endregion

        //-----------------------------------------------------------------------------------------------

        #region tab ข้อมูลรายงาน

        protected void LoadGridReport()
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("Seq", typeof(int));
            //dt.Columns.Add("ReportName", typeof(string));
            //dt.Rows.Add(0, "รายงาน 1");
            //dt.Rows.Add(1, "รายงาน 2");
            //Grid_Report.DataSource = dt;
            //Grid_Report.DataBind();
        }

        protected void Grid_Report_ItemCommand(object source, GridCommandEventArgs e)
        {
            //LoadGridReport();
        }

        #endregion

        #region ความคืบหน้า

        ProgressPrePurchaseRequestBLL bllPpr = new ProgressPrePurchaseRequestBLL();

        protected void LoadGridApprove()
        {
            //IList<PrePurchaseRequestRouteInfo> InfoPprRouteList = bllPpr.GetPrePurchaseRequestApprove((int)this.info.Seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar1").FindControl("GridApprove");
            //TmpGrid.DataSource = InfoPprRouteList;
            //TmpGrid.DataBind();
        }

        protected void LoadGridApproveHistory()
        {
            //IList<PrePurchaseRequestRouteHistoryInfo> InfoPprRouteHisList = bllPpr.GetPrePurchaseRequestApproveHistory((int)this.info.Seq);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar1").FindControl("GridHistory");
            //TmpGrid.DataSource = InfoPprRouteHisList;

            //GridGroupByExpression g;
            //TmpGrid.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(TmpGrid.Columns[0]);
            //TmpGrid.MasterTableView.GroupByExpressions.Add(g);

            //TmpGrid.Columns[0].Visible = false;
            //TmpGrid.DataBind();
        }

        protected void LoadGridInformHistory()
        {
            //IList<PrePurchaseRequestRouteHistoryInfo> InfoPprRouteHisList = bllPpr.GetPrePurchaseRequestInformHistory((int)this.info.Seq);
            //RadGrid TmpGrid2 = (RadGrid)RadPanelBar1.FindItemByValue("Bar1").FindControl("GridInformHis");
            //GridGroupByExpression g;
            //TmpGrid2.DataSource = InfoPprRouteHisList;
            //TmpGrid2.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(TmpGrid2.Columns[0]);
            //TmpGrid2.MasterTableView.GroupByExpressions.Add(g);

            //TmpGrid2.Columns[0].Visible = false;
            //TmpGrid2.DataBind();
        }

        protected void LoadGridInform()
        {
            //IList<PrePurchaseRequestRouteInfo> InfoPprRouteList = bllPpr.GetPrePurchaseRequestInform((int)this.info.Seq);
            //RadGrid TmpGrid2 = (RadGrid)RadPanelBar1.FindItemByValue("Bar1").FindControl("GridInform");
            //GridGroupByExpression g;
            //TmpGrid2.DataSource = InfoPprRouteList;
            //TmpGrid2.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(TmpGrid2.Columns[0]);
            //TmpGrid2.MasterTableView.GroupByExpressions.Add(g);

            //TmpGrid2.Columns[0].Visible = false;
            //TmpGrid2.DataBind();
        }

        protected void LoadGridRequest()
        {
            //IList<PrePurchaseRequestInfo> InfoPpr = bllPpr.GetPrePurchaseRequest(1);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar2").FindControl("GridRequest");
            //TmpGrid.DataSource = InfoPpr;
            //TmpGrid.DataBind();
        }

        protected void LoadGridPublishing()
        {
            //IList<BidInfo> InfoBid = bllPpr.GetBid(1);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar3").FindControl("GridPublishing");
            //TmpGrid.DataSource = InfoBid;
            //TmpGrid.DataBind();
        }

        protected void LoadGridCommand()
        {
            //IList<PurchaseOrderInfo> InfopurOrder = bllPpr.GetPurchaseOrder(1);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar4").FindControl("GridCommand");
            //TmpGrid.DataSource = InfopurOrder;
            //TmpGrid.DataBind();
        }

        protected void LoadGridCheck()
        {
            //IList<DeliveryNoteInfo> InfoDeliveryNote = bllPpr.GetDeliveryNote(1);
            //RadGrid TmpGrid = (RadGrid)RadPanelBar1.FindItemByValue("Bar5").FindControl("GridCheck");
            //TmpGrid.DataSource = InfoDeliveryNote;
            //TmpGrid.DataBind();
        }

        protected void GridApprove_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridApprove();
        }

        protected void GridHistory_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridApproveHistory();
        }

        protected void GridInformHis_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridInformHistory();
        }

        protected void GridInform_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridInform();
        }

        protected void GridRequest_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridRequest();
        }

        protected void GridRequest_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    GridDataItem dataBoundItem = e.Item as GridDataItem;
            //    Control target = e.Item.FindControl("lkName");
            //    Control targetRefNo = e.Item.FindControl("lkRefNo");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.TipApproveDetail, null))
            //        {
            //            this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":prr", true);
            //            this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":prr", true);
            //        }
            //    }
            //}
        }

        protected void GridPublishing_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridPublishing();
        }

        protected void GridPublishing_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    GridDataItem dataBoundItem = e.Item as GridDataItem;
            //    Control target = e.Item.FindControl("lkName");
            //    Control targetRefNo = e.Item.FindControl("lkRefNo");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.TipApproveDetail, null))
            //        {
            //            this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":br", true);
            //            this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":br", true);
            //        }
            //    }
            //}
        }

        protected void GridCommand_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridCommand();
        }

        protected void GridCommand_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    GridDataItem dataBoundItem = e.Item as GridDataItem;
            //    Control target = e.Item.FindControl("lkName");
            //    Control targetRefNo = e.Item.FindControl("lkRefNo");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.TipApproveDetail, null))
            //        {
            //            this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":por", true);
            //            this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":por", true);
            //        }
            //    }
            //}
        }

        protected void GridCheck_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridCheck();
        }

        protected void GridCheck_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    GridDataItem dataBoundItem = e.Item as GridDataItem;
            //    Control target = e.Item.FindControl("lkName");
            //    Control targetRefNo = e.Item.FindControl("lkRefNo");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.TipApproveDetail, null))
            //        {
            //            this.TipApproveDetail.TargetControls.Add(target.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":dnr", true);
            //            this.TipApproveDetail.TargetControls.Add(targetRefNo.ClientID, "Item:" + dataBoundItem["Ref_no"].Text + ":dnr", true);
            //        }
            //    }
            //}
        }

        protected void OnAjaxUpdate_AppDet(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip_AppDet(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip_AppDet(string elementID, UpdatePanel panel)
        {
            string[] all_value = elementID.Split(new string[] { ":" }, StringSplitOptions.None);
            //if (all_value[0] == "Item")
            //{
            //    Session["Resource_Item_Id"] = all_value[1];
            //    Session["Expense_Id"] = null;
            //}
            //else
            //{
            //    Session["Resource_Item_Id"] = null;
            //    Session["Expense_Id"] = all_value[1];
            //}
            Session["RefNo"] = all_value[1];
            Session["DisType"] = all_value[2];
            Control ctrl = Page.LoadControl("../UserControl/TIP_Approve_Detail.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void OnAjaxUpdate_Approve_History(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip_Approve_History(args.Value, args.UpdatePanel);
        }

        protected void OnAjaxUpdate_Inform_History(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip_Inform_History(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip_Approve_History(string elementID, UpdatePanel panel)
        {
            Session["HisSeq"] = elementID;
            Control ctrl;
            ctrl = Page.LoadControl("../UserControl/ICM_PPR_RouteHistoryApprove.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        private void UpdateToolTip_Inform_History(string elementID, UpdatePanel panel)
        {
            Session["HisSeq"] = elementID;
            Control ctrl;
            ctrl = Page.LoadControl("../UserControl/ICM_PPR_RouteHistoryInform.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void RefreshProgress()
        {
            //int seq = Convert.ToInt32(Request.QueryString["Seq"]);
            //info = bll.GetPrePurchaseRequest(Convert.ToInt32(seq));
            //Session["info"] = info;

            //IBLLSysProgress bllProg = new SysProgressBLL();
            //SysProgressInfo InfoProg = new SysProgressInfo();
            //InfoProg = bllProg.GetSysProgress(seq);
            //lblProgress.Text = InfoProg.Name;

            //LoadGridApprove();
            //LoadGridApproveHistory();
            //LoadGridInformHistory();
            //LoadGridInform();
            //LoadGridRequest();
            //LoadGridPublishing();
            //LoadGridCommand();
            //LoadGridCheck();
            //LoadGrid_Reject();
            //ManageDisplay();
        }

        protected void btnHistory_Click(object sender, EventArgs e)
        {

        }

        protected void btnHistory2_Click(object sender, EventArgs e)
        {

        }

        #endregion
    }
}
